﻿using System;
using System.Data;
using System.Windows.Forms;

namespace PRE
{

    public partial class frmquanliquay : Form
    {
        DAL.EntitiesDAL.ban_DAL _dal = new DAL.EntitiesDAL.ban_DAL();
        //BLL.NewFolder1.chatlieuBLL _bll = new BLL.NewFolder1.chatlieuBLL();
        public frmquanliquay()
        {
            InitializeComponent();
        }
        protected DataTable _dtb = new DataTable("Tmp");

        private void Form1_Load(object sender, EventArgs e)
        {
            loadban();
        }

        void loadban()
        {
            lvwTDofBan.DataBindings.Clear();
            _dtb = _dal.Select();
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in dlll.Rows)
                {
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                    }
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }

        void LoadTree(DataTable tbl, TreeView tre)
        {
            //TreeNode NodeCha, NodeCon;
            //treTD.Nodes.Clear();
            //foreach (DataRow drw in tbl.Rows)
            //{
            //    NodeCha = new TreeNode();
            //    NodeCha.ForeColor = Color.Red;
            //    NodeCha.Text = drw["ten_loai"].ToString();
            //    NodeCha.Tag = drw;
            //    foreach (DataRow drwCon in drw.GetChildRows("loai_mon_mon"))
            //    {
            //        NodeCon = new TreeNode();
            //        NodeCon.Text = drwCon["ten_mon"].ToString();
            //        NodeCon.Tag = drwCon;
            //        NodeCha.Nodes.Add(NodeCon);
            //    }
            //    tre.Nodes.Add(NodeCha);
            //}
        }

        public void DataBindingControl()
        {
            //txtMaCL.DataBindings.Add("EditValue", _dtb, ".MA_CL");
            //txtTenCL.DataBindings.Add("EditValue", _dtb, ".TEN_CL");
            //base.DataBindingControl();
        }

        private void dschatlieu_Click(EventArgs e)
        {
            //DataBindingControl();
        }

        private void dockPanel3_Click(object sender)
        {

        }

        private void treTD_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdtaoban_Click(object sender, EventArgs e)
        {
            frmGoimon frm = new frmGoimon();
            frm.ShowDialog();
        }


        private void cmdtinhtien_Click(object sender, EventArgs e)
        {
            frmphieu frm0 = new frmphieu();
            frm0.ShowDialog();
        }

       

        private void lvBan_Click(object sender, EventArgs e)
        {
            lvwTDofBan.Items.Clear();
            _dtb = _dal.Select();
            ListViewItem lvwitem = new ListViewItem();
            lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
            if (lvwitem.ImageIndex == 1)
            {
                foreach (DataRow drw in _dtb.Rows)
                {
                    if (drw["MaBan"].ToString() == lvwitem.Tag.ToString())
                    {
                        DataTable _hien = _dal.hien_thi(drw["MABAN"].ToString());
                       
                        lvwTDofBan.Columns.Clear();
                        lvwTDofBan.Clear();
                        lvwTDofBan.Columns.Add("Tên món");
                        lvwTDofBan.Columns[0].Width = 110;
                        lvwTDofBan.Columns.Add("Đơn giá");
                        lvwTDofBan.Columns[1].Width = 100;
                        lvwTDofBan.Columns.Add("Số lượng");
                        lvwTDofBan.Columns[2].Width = 70;
                        lvwTDofBan.Columns.Add("Tính tiền");
                        lvwTDofBan.Columns[3].Width = 100;
                        //lvwTDofBan.Columns.Add("Giảm giá (%)");
                        //lvwTDofBan.Columns[4].Width = 100;
                        //int Tong = 0;
                        foreach (DataRow row1 in _hien.Rows)
                        {
                            if (row1["tinhtien"].ToString() != null)
                            {
                                ListViewItem lvwitem1 = new ListViewItem();
                                lvwitem1.Text = row1["TenMon"].ToString();
                                lvwitem1.SubItems.Add(row1["DonGia"].ToString());
                                lvwitem1.SubItems.Add(row1["SoLuong"].ToString());
                                lvwitem1.SubItems.Add(row1["tinhtien"].ToString());
                                //lvwitem1.SubItems.Add(row1["GiamGia"].ToString());
                                lvwTDofBan.Items.Add(lvwitem1);
                                //Tong += ((Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"]))-(Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"])*Convert.ToInt32(row1["GiamGia"])/100));
                            }
                            else
                            { }


                            //lblTong.Text = Tong.ToString();

                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lvBan.Items.Clear();
            _dtb = _dal.Select();
            DataTable dlll = _dal.load();
         
            foreach (DataRow drw in _dtb.Rows)
            {
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
               
                foreach (DataRow row in dlll.Rows)
                {
                    
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                    }
                    //else
                    //{
                    //    lvwitem.ImageIndex = 0;
                    //    hinh = 0;

                    //}
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }
        //
    }
}
 
